#pragma once

#include <iostream>
#include <string>
#include <jsoncpp/json/json.h>

using namespace std;

const string sep = "\r\n";
// 添加长度
bool Encode(string &message)
{
    if (message.size() == 0)
        return false;
    string len = to_string(message.size());
    message = len + sep + message + sep;
    return true;
}

bool Decode(string &package,string *content)
{
    auto pos=package.find(sep);
    if(pos==string::npos) return false;
    string content_len_str = package.substr(0, pos);
    int content_len=stoi(content_len_str);
    int full_len=content_len+2*sep.size()+content_len_str.size();
    if(package.size()<full_len) return false;
    *content=package.substr(pos+sep.size(),content_len);
    package.erase(0,full_len);
    return true;
}


class Request
{
private:
    int _x;
    int _y;
    char _oper;

public:
    Request() : _x(0), _y(0), _oper(0) {}
    Request(int x, int y, char oper) : _x(x), _y(y), _oper(oper) {}
    bool serialize(string &out_string)
    {
        Json::Value root;
        root["x"] = _x;
        root["y"] = _y;
        root["oper"] = _oper;
        Json::StreamWriterBuilder wb;
        std::unique_ptr<Json::StreamWriter> w(wb.newStreamWriter());
        std::stringstream ss;
        w->write(root, &ss);
        out_string = ss.str();
        return true;
    }
    bool deserialize(string &in_string)
    {
        Json::Value root;
        Json::Reader reader;
        bool parsingSuccessful = reader.parse(in_string, root);
        if (!parsingSuccessful)
        {
            std::cout << "Failed to parse JSON: " << reader.getFormattedErrorMessages() << std::endl;
            return false;
        }

        _x = root["x"].asInt();
        _y = root["y"].asInt();
        _oper = root["oper"].asInt();

        return true;
    }
    void Print()
    {
        std::cout << _x << std::endl;
        std::cout << _oper << std::endl;
        std::cout << _y << std::endl;
    }
    int X() const { return _x; }
    int Y() const { return _y; }
    char Oper() const { return _oper; }
};

class Response
{
public:
    Response() : _result(0), _code(0)
    {
    }
    Response(int result, int code) : _result(result), _code(code)
    {
    }
    bool Serialize(std::string &out_string)
    {
        Json::Value root;
        root["result"] = _result;
        root["code"] = _code;

        Json::StreamWriterBuilder wb;
        std::unique_ptr<Json::StreamWriter> w(wb.newStreamWriter());
        std::stringstream ss;
        w->write(root, &ss);
        out_string = ss.str();
        return true;
    }
    bool Deserialize(std::string &in_string)
    {
        Json::Value root;
        Json::Reader reader;
        bool parsingSuccessful = reader.parse(in_string, root);
        if (!parsingSuccessful)
        {
            std::cout << "Failed to parse JSON: " << reader.getFormattedErrorMessages() << std::endl;
            return false;
        }

        _result = root["result"].asInt();
        _code = root["code"].asInt();

        return true;
    }
    int Result() const { return _result; }
    int Code() const { return _code; }
    void SetResult(int res) { _result = res;}
    void SetCode(int c) {_code = c;}
private:
    int _result; // 结果
    int _code;   // 出错码，0,1,2,3,4
};